
using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
using Csla.Validation;
using SariturIntranetLibrary.Configuracoes;

namespace SariturIntranetLibrary.Grupos
{
    [Serializable()]
    public class GrupoUsuarioChildInfo : Csla.ReadOnlyBase<GrupoUsuarioChildInfo>
    {

        #region Business Properties and Methods

        //declare members

        private Guid _id = Guid.NewGuid();

        private int _index = 0;

        private string _nomeDoGrupo = string.Empty;

        private int _tipoDeUsuarioId = 1;

        private string _tipoDeUsuarioString = string.Empty;

        private bool _selecionado = false;

        private bool _selecionadoNaBase = false;

        private Guid _usuarioId = Guid.Empty;

        [System.ComponentModel.DataObjectField(true, false)]
        public Guid Id
        {
            get
            {
                CanReadProperty("Id", true);
                return _id;
            }
        }

        public int Index
        {
            get
            {
                CanReadProperty("Index", true);
                return _index;
            }
        }

        public string NomeDoGrupo
        {
            get
            {
                CanReadProperty("NomeDoGrupo", true);
                return _nomeDoGrupo;
            }
        }

        public bool Selecionado
        {
            get
            {
                CanReadProperty("Selecionado", true);
                return _selecionado;
            }
            set
            {
                if (!_selecionado.Equals(value))
                {
                    _selecionado = value;
                }
            }
        }

        public int TipoDeUsuarioId
        {
            get
            {
                CanReadProperty("TipoDeUsuarioId", true);
                return _tipoDeUsuarioId;
            }
        }

        public string TipoDeUsuarioString
        {
            get
            {
                CanReadProperty("TipoDeUsuarioString", true);
                return _tipoDeUsuarioString;
            }
        }

        protected override object GetIdValue()
        {
            return _id;
        }

        #endregion //Business Properties and Methods

        #region Authorization Rules
        protected override void AddAuthorizationRules()
        {
            //TODO: Define authorization rules in GrupoUsuarioChildInfo
            //AuthorizationRules.AllowRead("Id", "GrupoUsuarioChildInfoReadGroup");
            //AuthorizationRules.AllowRead("NomeDoGrupo", "GrupoUsuarioChildInfoReadGroup");
            //AuthorizationRules.AllowRead("TipoDeUsuarioId", "GrupoUsuarioChildInfoReadGroup");
            //AuthorizationRules.AllowRead("TipoDeUsuarioString", "GrupoUsuarioChildInfoReadGroup");

        }

        #endregion //Authorization Rules

        #region Factory Methods
        internal static GrupoUsuarioChildInfo GetGrupoUsuarioChildInfo(SafeDataReader dr, Guid usuarioId, int index)
        {
            return new GrupoUsuarioChildInfo(dr, usuarioId, index);
        }

        private GrupoUsuarioChildInfo(SafeDataReader dr, Guid usuarioId, int index)
        {
            Fetch(dr, usuarioId, index);
        }
        #endregion //Factory Methods


        #region Data Access

        #region Data Access - Fetch
        private void Fetch(SafeDataReader dr, Guid usuarioId, int index)
        {
            FetchObject(dr, usuarioId, index);

            //load child object(s)
            FetchChildren(dr);

        }

        private void FetchObject(SafeDataReader dr, Guid usuarioId, int index)
        {
            _id = dr.GetGuid("Id");
            _nomeDoGrupo = dr.GetString("NomeDoGrupo");
            object obj = dr["TipoDeUsuarioId"];
            _usuarioId = usuarioId;
            _index = index;
            if (obj != null)
            {
                _tipoDeUsuarioId = dr.GetInt32("TipoDeUsuarioId");
                _tipoDeUsuarioString = dr.GetString("TipoDeUsuarioString");
                _selecionado = true;
                _selecionadoNaBase = true;
            }
        }

        private void FetchChildren(SafeDataReader dr)
        {
        }

        internal void Salvar()
        {
            if (_selecionado)
            {
                if (!_selecionadoNaBase)
                {
                    using (SqlConnection cn = new SqlConnection(ConfigDatabase.SariturDBConnection))
                    {
                        cn.Open();

                        using (SqlCommand cm = cn.CreateCommand())
                        {
                            cm.CommandType = CommandType.StoredProcedure;
                            cm.CommandText = "AddUsuarioGrupoDePermissao";

                            cm.Parameters.AddWithValue("@UsuarioId", _usuarioId);
                            cm.Parameters.AddWithValue("@GrupoDePermissaoId", _id);
                            cm.Parameters.AddWithValue("@TipoDeUsuario", _tipoDeUsuarioId);

                            cm.ExecuteNonQuery();
                        }
                    }//using
                    _selecionadoNaBase = _selecionado;
                }
            }
            else
            {
                if (_selecionadoNaBase)
                {
                    using (SqlConnection cn = new SqlConnection(ConfigDatabase.SariturDBConnection))
                    {
                        cn.Open();

                        using (SqlCommand cm = cn.CreateCommand())
                        {
                            cm.CommandType = CommandType.StoredProcedure;
                            cm.CommandText = "DeleteUsuarioGrupoDePermissao";

                            cm.Parameters.AddWithValue("@UsuarioId", _usuarioId);
                            cm.Parameters.AddWithValue("@GrupoDePermissaoId", _id);

                            cm.ExecuteNonQuery();
                        }
                    }//using
                    _selecionadoNaBase = _selecionado;
                }
            }

        }
        #endregion //Data Access - Fetch
        #endregion //Data Access
    }
}
